/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.view.dialog 
{
	import net.pixlib.commands.Delegate;
	import net.pixlib.core.PixlibApplication;
	import net.pixlib.plugin.Plugin;
	import net.pixlib.prefs.Preferences;
	import net.pixlib.sfe.log.Debug;
	import net.pixlib.sfe.util.PrefNameList;
	import net.pixlib.sfe.util.getPreferences;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.sfe.view.ViewList;
	import net.pixlib.sfe.view.abstract.JFrameView;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;

	import org.aswing.AsWingConstants;
	import org.aswing.BorderLayout;
	import org.aswing.Box;
	import org.aswing.Insets;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JLabelButton;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	import org.aswing.LoadIcon;
	import org.aswing.SoftBox;
	import org.aswing.SoftBoxLayout;
	import org.aswing.border.EmptyBorder;

	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;

	/**
	 * The 'Preferences' dialog view.
	 * 
	 * @author Romain Ecarnot
	 */
	public class OptionView extends JFrameView
	{

		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private var _labelLang : JLabel; 

		/** @private */
		private var _labelSDK : JLabel;

		/** @private */
		private var _buttonSDK : JButton; 

		/** @private */
		private var _inputSDK : JTextField;

		/** @private */
		private var _labelFont : JLabel;

		/** @private */
		private var _buttonFont : JButton; 

		/** @private */
		private var _inputFont : JTextField;

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates view.
		 */		
		public function OptionView(owner : Plugin)
		{
			super(owner, ViewList.OPTION_VIEW, new JFrame(PixlibApplication.getInstance().root, "Preferences", true));
		}

		/**
		 * Overrides View#show() method to deal with JFrame component.
		 */
		override public function show() : void
		{
			var prefs : Preferences = getPreferences();
			
			_inputSDK.setText(prefs.getValue(PrefNameList.SDK_PATH, ""));			_inputFont.setText(prefs.getValue(PrefNameList.FONT_PATH, ""));
			
			getFrame().pack();
			getFrame().setX(PixlibApplication.getInstance().size.width * 0.5 - getFrame().width * 0.5);			getFrame().setY(PixlibApplication.getInstance().size.height * 0.5 - getFrame().height * 0.5);
			getFrame().show();
			
			super.show();
		}

		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
		
		/**
		 * Inits User Interface.
		 */
		override protected function onInitView( ) : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			getFrame().setContentPane(new SoftBox(SoftBoxLayout.Y_AXIS, 5));
			getFrame().setResizable(false);
			getFrame().filters = [new DropShadowFilter(0, 0, 0x000000, 0.75, 10, 10, 1, BitmapFilterQuality.MEDIUM)];
			
			getPanel().setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
			
			_labelLang = new JLabel("Language", null, JLabel.LEFT);
			_labelLang.setPreferredWidth(75);
			
			var lBox : SoftBox = new SoftBox(SoftBoxLayout.X_AXIS, 10, AsWingConstants.LEFT);
			
			var len : uint = LocalManager.getInstance().getAvailableLocales().length;
			var flagButton : JLabelButton;
			var locale : String;
			for(var i : uint = 0;i < len; i++)
			{
				locale = LocalManager.getInstance().getAvailableLocales()[i];
				flagButton = new JLabelButton("", new LoadIcon("lang/" + locale + "/flag.png"), JLabel.LEFT);
				flagButton.addActionListener(Delegate.create(_onChangeLangHandler, locale));	
				
				lBox.append(flagButton);
			}
			
			var langLine : JPanel = new JPanel(new BorderLayout(5, 5));
			langLine.append(_labelLang, BorderLayout.WEST);
			langLine.append(lBox, BorderLayout.CENTER);
			
			_labelSDK = new JLabel("Flex SDK", null, JLabel.LEFT);
			_labelSDK.setPreferredWidth(75);
			
			_buttonSDK = new JButton("browse");
			_buttonSDK.setToolTipText("Flex SDK path");
			_buttonSDK.addActionListener(_onClickSDKHandler);
			_buttonSDK.setPreferredWidth(60);
			
			_inputSDK = new JTextField();
			_inputSDK.setEditable(false);
			_inputSDK.setPreferredWidth(200);
			
			var sdkLine : JPanel = new JPanel(new BorderLayout(5, 5));
			sdkLine.append(_labelSDK, BorderLayout.WEST);			sdkLine.append(_inputSDK, BorderLayout.CENTER);
			sdkLine.append(_buttonSDK, BorderLayout.EAST);
			
			_labelFont = new JLabel("Font path", null, JLabel.LEFT);
			_labelFont.setPreferredWidth(75);
			
			_buttonFont = new JButton("browse");
			_buttonFont.setToolTipText("default font path");
			_buttonFont.addActionListener(_onClickFontHandler);
			_buttonFont.setPreferredWidth(60);
			
			_inputFont = new JTextField();
			_inputFont.setEditable(false);
			_inputFont.setPreferredWidth(200);
			
			var fontLine : JPanel = new JPanel(new BorderLayout(5, 5));
			fontLine.append(_labelFont, BorderLayout.WEST);
			fontLine.append(_inputFont, BorderLayout.CENTER);
			fontLine.append(_buttonFont, BorderLayout.EAST);
			
			getPanel().append(langLine);
			getPanel().append(Box.createVerticalBox(10));			getPanel().append(sdkLine);			getPanel().append(fontLine);
			
			_onLocalChangeHandler();
			
			super.onInitView();
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @private */
		private function _onLocalChangeHandler(event : LocalManagerEvent = null) : void
		{
			getFrame().setTitle(getTranslation("optionDialogTitle"));
			
			_labelLang.setText(getTranslation("optionLangLabel"));
			
			_labelSDK.setText(getTranslation("optionSDKLabel"));			_buttonSDK.setText(getTranslation("optionSDKButton"));			_buttonSDK.setToolTipText(getTranslation("optionSDKTip"));
			
			_labelFont.setText(getTranslation("optionFontLabel"));
			_buttonFont.setText(getTranslation("optionFontButton"));
			_buttonFont.setToolTipText(getTranslation("optionFontTip"));
		}

		/** @private */
		private function _onChangeLangHandler(event : Event, lang : String) : void
		{
			LocalManager.getInstance().locale = lang; 
			
			var prefs : Preferences = getPreferences();
			prefs.setValue(PrefNameList.LANGUAGE, lang);
			prefs.save();	
		}

		/** @private */
		private function _onClickSDKHandler(event : Event) : void
		{
			var directory : File = File.documentsDirectory;

			try
			{
				directory.browseForDirectory("Flex SDK Home");
				directory.addEventListener(Event.SELECT, _onSDKDirectorySelectedHandler);
			}
			catch (error : Error)
			{
				directory.removeEventListener(Event.SELECT, _onSDKDirectorySelectedHandler);
				Debug.ERROR("Failed: " + error.message);
			}
		}

		/** @private */
		private function _onSDKDirectorySelectedHandler(event : Event) : void 
		{
			event.target.removeEventListener(Event.SELECT, _onSDKDirectorySelectedHandler);
			
			_inputSDK.setText(File(event.target).nativePath);
			
			var prefs : Preferences = getPreferences();
			prefs.setValue(PrefNameList.SDK_PATH, _inputSDK.getText());
			prefs.save();
		}

		/** @private */
		private function _onClickFontHandler(event : Event) : void
		{
			var directory : File = File.documentsDirectory;

			try
			{
				directory.browseForDirectory("Font path");
				directory.addEventListener(Event.SELECT, _onFontDirectorySelectedHandler);
			}
			catch (error : Error)
			{
				directory.removeEventListener(Event.SELECT, _onFontDirectorySelectedHandler);
				Debug.ERROR("Failed: " + error.message);
			}
		}

		/** @private */
		private function _onFontDirectorySelectedHandler(event : Event) : void 
		{
			event.target.removeEventListener(Event.SELECT, _onFontDirectorySelectedHandler);
			
			_inputFont.setText(File(event.target).nativePath);
			
			var prefs : Preferences = getPreferences();
			prefs.setValue(PrefNameList.FONT_PATH, _inputFont.getText());
			prefs.save();
		}
	}
}
